import moment from 'moment'
import config from './config.js'
import util from './util.js'


export function formatImgPath(str, width, height){
	const telent = util.isTenant()
	if(!str){
		return str;
	}
	if(str.startsWith('http')){
		return str.split('&')[0];
	} else if (str.startsWith('/static')){
		return str;
	} else if (str.startsWith(';')){
		let strs = str.split(';');
		for (let s of strs) {
			if (!s) continue;
			return formatImgPath(s, width, height)
		}
	}  else {
		// let apiConnect = ''
		// if(telent==='TENANT' || telent==='NOT_YET_TENANT'){
		// 	apiConnect = 'wx'
		// }else{
		// 	apiConnect = 'app'
		// }
		let url = `${config.BASE_URL}/app/mySelf/getPic?fileName=${str}`;
		console.log(2222,str)
		if (util.getStorage("loginInfo").app.token) {
			 url +='&token=' + util.getStorage("loginInfo").app.token
		}
		if (width) {
			url += '&width=' + width
		}
		if (height) {
			url += '&height=' + height
		}
		// util.get({
		// 	url:'app/mySelf/getPic?fileName=1730164668311711744'
		// }).then((res)=>{console.log(4444,url = res)})
		
		return url
	}
}

export function formatNumber(str) {
	if (!str) {
		return str;
	}
	str = str.toString();
	let intNum = str.replace(/\B(?=(?:\d{3})+$)/g, ',');
	return intNum;
}

export function formatMoney(str) {
	if (!str) {
		return str;
	}
	str = str.toString();
	if (str === '0') {
		return '0.00';
	}
	if (str.indexOf('.') === 0) {
		str = '0' + str;
	}
	if (str.substr(-1) === '.') {
		str = str + '00';
	}
	str = str.indexOf('.') >= 0 ? str.replace(/\.\d*/i, (xiaoshu) => {
		return (xiaoshu.length === 2 ? xiaoshu + '0' : xiaoshu.slice(0, 3));
	}) : str + '.00';
	let intNum = str.substring(0, str.indexOf('.')).replace(/\B(?=(?:\d{3})+$)/g, ',');
	let dot = str.substring(str.indexOf('.'), str.length);
	return intNum + dot;
}

export function formatMoneyInt(str) {
	str = formatMoney(str) + ""

	return str.split(".")[0];
}

export function formatMoneyNo00(str) {
	if(!str || str == '0'){
		return ''
	}
	str = formatMoney(str) + ""
	if (str.indexOf(".00") >= 0 || str.indexOf(".0") >= 0) {
		return str.split(".")[0];
	} else {
		return str;
	}
}

export function formatPhone(tel){
	if(tel){
		let reg = /^(\d{3})[\s\S]+(\w{4})$/;
		let phoneNum = tel.toString().replace(reg, '$1 **** $2');
		return phoneNum;
	}
	return tel
}

export function formatDate(date) {
	return moment(date).format('YYYY-MM-DD');
}

export function formatMMDD(date){
	return moment(date).format('MM月DD日');
}

export function formatDateTime(date){
	return moment(date).format('MM月DD日 HH:mm');
}

export function formatPercent(str){
	if(!str){
		return 0
	}
	return (parseFloat(str) * 100).toFixed(0);
}
